﻿using DispatchingService.CustomJob;
using DispatchingService.CustomListener;
using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DispatchingService
{
    public class DispatchingManager
    {

        /// <summary>
        /// 
        /// </summary>
        public async static Task Init()
        {
            #region 创建单元（时间轴/载体）
            StdSchedulerFactory factory = new StdSchedulerFactory();
            IScheduler scheduler = await factory.GetScheduler();
            await scheduler.Start();
            #endregion

            #region Listener

            scheduler.ListenerManager.AddJobListener(new CustomJobListener());
            scheduler.ListenerManager.AddTriggerListener(new CustomTriggerListener());
            scheduler.ListenerManager.AddSchedulerListener(new CustomSchedulerListener());
            
            #endregion




            //Job(作业)
            IJobDetail jobDetail = JobBuilder.Create<SendMessageJob>()
                                             .WithIdentity("sendMessageJob", "group1")
                                             .WithDescription("this is Send Message Job")
                                             .Build();
            jobDetail.JobDataMap.Add("student1", "张三");//传入参数
            jobDetail.JobDataMap.Add("student2", "李四");//传入参数
            jobDetail.JobDataMap.Add("student3", "王五");//传入参数
            jobDetail.JobDataMap.Add("Year", DateTime.Now.Year);//传入参数




            //Trigger(时间策略)
            ITrigger trigger = TriggerBuilder.Create()
                                             .WithIdentity("sendMessageTrigger", "group1")
                                             //.StartAt(new DateTimeOffset())//从某一个时间点开始
                                             //.StartNow()//马上执行一次
                                             .WithSimpleSchedule(w => w.WithIntervalInSeconds(2)
                                             //.WithRepeatCount(2)
                                             .RepeatForever())
                                             .Build();

            trigger.JobDataMap.Add("student4", "赵六");//传入参数
            trigger.JobDataMap.Add("student5", "田七");//传入参数
            trigger.JobDataMap.Add("student6", "Richard");//传入参数
            trigger.JobDataMap.Add("Year", DateTime.Now.Year + 1);//传入参数
             
            await scheduler.ScheduleJob(jobDetail, trigger);
        }
    }
}
